package com.hexu.event.view;

import com.hexu.event.bean.MemconBean;
import com.hexu.event.entity.Memcon;
import com.hexu.event.util.StageUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

import static com.hexu.event.util.ServiceUtil.memconService;

public class MemconTableView {

    static String selectPhoneNumber = null;
    static String selectContent = null;
    static String selectReply = null;
    static Integer selectIndex = null;

    public static void memconView(int visitorsOrUsersOrAdministrator, String phoneNumber, int count, List<Memcon> list) {
        List<MemconBean> memconBeanList = new ArrayList<>();

        TableView<MemconBean> table = new TableView<>();

        for(int i = 0;i<count;i++){
            //添加这些信息
            memconBeanList.add( new MemconBean(list.get(i).getPhoneNumber(),list.get(i).getContent(),
                    list.get(i).getReply())
            );
        }
        //转化成表可存放的数据
        ObservableList<MemconBean> data =
                FXCollections.observableArrayList(
                        memconBeanList
                );


        //用户手机号码
        //列的数据类型
        TableColumn<MemconBean,String> phoneNumberCol =
                new TableColumn<>("手机号码");
        //设置出是最小宽度
        phoneNumberCol.setMinWidth(125);
        phoneNumberCol.setCellValueFactory(
                new PropertyValueFactory<>("phoneNumber"));
        //用户申诉建议内容
        TableColumn<MemconBean, String> contentCol =
                new TableColumn<>("申诉建议");
        contentCol.setMinWidth(155);
        contentCol.setCellValueFactory(
                new PropertyValueFactory<>("content"));
        //管理员回复
        TableColumn<MemconBean,String> replyCol = new TableColumn<>("回复");
        replyCol.setMinWidth(155);
        replyCol.setCellValueFactory(
                new PropertyValueFactory<>("reply"));

        //接收信息
        table.setItems(data);
        //把数据放在相应列里
        table.getColumns().addAll(phoneNumberCol,contentCol,replyCol);

        VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));

        HBox hb = new HBox();
        hb.setSpacing(10);
        Label labelContent = new Label("申诉及建议：");
        TextField textFieldContent = new TextField();
        Button sureContent = new Button("确定提交");
        //如果是用户登录
        if(visitorsOrUsersOrAdministrator == 1){
            hb.getChildren().addAll(labelContent,textFieldContent,sureContent);
        }

        TextField textFieldReply = new TextField();
        Button sureReply = new Button("确定回复");

        if(visitorsOrUsersOrAdministrator == 2){
            hb.getChildren().addAll(textFieldReply,sureReply);
        }

        vbox.getChildren().addAll(table, hb);
        StageUtil.showStage(new Stage(),new Scene(vbox),550,460,"用户申诉及建议",false);

        //设置点击事件
        table.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //获取序列
                selectIndex = table.getSelectionModel().getSelectedIndex();
                //如果点击的时候不是点击空处
                if(selectIndex != -1){
                    //获取选择的信息
                    selectPhoneNumber = table.getSelectionModel().getSelectedItems().get(0).getPhoneNumber();
                    selectContent = table.getSelectionModel().getSelectedItems().get(0).getContent();
                    selectReply = table.getSelectionModel().getSelectedItems().get(0).getReply();
                }
            }
        });

        //设置用户 确定提交 的点击事件
        sureContent.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                sureContent(textFieldContent.getText(),phoneNumber,data);
            }
        });

        //设置管理员 确定回复 的点击事件
        sureReply.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //如果点击的时候不是点击空处
                if(selectIndex != -1 && selectIndex != null){
                    sureReply(textFieldReply.getText(),table);
                }
            }
        });

    }

    //用户提交申诉建议
    public static void sureContent(String textFieldContent,String phoneNumber,ObservableList<MemconBean> data){
        //保存这些信息
        Memcon memcon = new Memcon(null,phoneNumber,textFieldContent,"");
        Boolean flag =  memconService.addContent(memcon);
        StageUtil.display(flag,new Stage(),"发送");
        //成功就更新表的内容
        if(flag){
            data.add(new MemconBean(phoneNumber,textFieldContent,""));
        }
    }

    //管理员回复
    public static void sureReply(String textFieldReply,TableView table){
        //保存这些信息
        Memcon memcon = new Memcon(null,selectPhoneNumber,selectContent,textFieldReply);
        Boolean flag =  memconService.addReply(memcon);
        StageUtil.display(flag,new Stage(),"回复");
        //成功就更新表的内容
        if(flag){
            table.getItems().set(selectIndex,new MemconBean(selectPhoneNumber,selectContent,textFieldReply));
        }

    }
}